package com.ilove.video.frame;

import java.io.IOException;
import java.util.Hashtable;

import javax.microedition.lcdui.Image;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.core.model.DataProvider;
import org.kalmeo.kuix.util.Metrics;
import org.kalmeo.kuix.widget.ListItem;
import org.kalmeo.kuix.widget.Picture;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.Text;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.kuix.widget.Screen.ScreenMenu;

import org.kalmeo.util.frame.Frame;

import com.ilove.media.model.CategoryList;
import com.ilove.media.model.MediaDataProvider;

import com.ilove.core.*;
import com.ilove.frame.*;

public class CategoryFrame implements Frame {

	public static CategoryFrame instance = new CategoryFrame();
	public int id;// 0 text data from vas, 1 ringtone, 2 game ,3 wallpaper
	public String code;
	public Hashtable preCategory = new Hashtable(); // luu a category vua chon menu truoc
	public String name;
	public String[] categorylist = null;
	public String[] vecContent = null;
	private Screen screen;
	private int level = 1;  // luu do sau cua category  1 tro ve main
	int currPage = 1;
	int pageSize = 8;
	int totalpage = 1;
	boolean isnext = true;
	private final MediaDataProvider mdp = new MediaDataProvider();
	
	public void showScreen() {
		if (screen != null) {
			screen.setCurrent();
		}
	}
	public void onAdded() {
		try {
			int total = mdp.getTotalCategory(code);
			totalpage = (int) Math.ceil((double)total/(double)pageSize);
			categorylist = mdp.getCategoryListWithPage(code,currPage,pageSize);

			if(categorylist ==null)
			{
				ContentListFrame.instance.id = id;
				ContentListFrame.instance.code = code;
				ContentListFrame.instance.name = name;
				Kuix.getFrameHandler().pushFrame(ContentListFrame.instance);
			}else
			{
				mdp.setCategoryList(categorylist);
				screen = Kuix.loadScreen(ILOVEContants.CATEGORY_SCREEN, mdp);
				screen.setTitle(name);
				screen.setCurrent();
			}
			Text textPage = (Text)screen.getWidget("textPage");
			textPage.setText(currPage + "/" + totalpage);
			int screenWidth = Kuix.getCanvas().getWidth();
			ScreenMenu rightMenu = (ScreenMenu)screen.getSecondMenu();
			Metrics metrics = rightMenu.getPreferredSize(screenWidth);
			int containerWidth = screenWidth - (2*metrics.width)-10;
			Widget container = screen.getWidget("CTM_PAGE");
			container.setAttribute("style", "layout:flowlayout(true, fill);align:center;min-size:"+containerWidth + " 16");
		} catch (Exception e) {
			Kuix.alert(e);
			e.printStackTrace();
		}
	}

	public boolean onMessage(Object identifier, Object[] arguments) {
		if ("showMainScreen".equals(identifier)) {
			showScreen();
			return false;
		} else if ("askQuit".equals(identifier)) {
			Kuix.alert(Kuix.getMessage("ASK_QUIT"),
					KuixConstants.ALERT_QUESTION | KuixConstants.ALERT_YES
							| KuixConstants.ALERT_NO, "!exit", null);
			return false;
		} 
		else if ("catefrmNext".equals(identifier)) {
			Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
			if (focusedWidget instanceof ListItem) {
				DataProvider dataprovider = ((ListItem) focusedWidget).getDataProvider();
				if (dataprovider != null) {
					CategoryList catelist = (CategoryList) dataprovider;
					
					try {

						categorylist = mdp.getCategoryListWithPage(catelist.categoryCode,currPage,pageSize);
						if(categorylist ==null)
						{
							ContentListFrame.instance.id = id;
							ContentListFrame.instance.code = catelist.categoryCode;
							ContentListFrame.instance.name = catelist.categoryName;
							Kuix.getFrameHandler().pushFrame(ContentListFrame.instance);
						}
						else
						{
							level++;
							preCategory.put(new Integer(level), catelist);
							mdp.setCategoryList(categorylist);
							screen.setTitle(catelist.categoryName);
							screen.setDataProvider(mdp);
						}
					} catch (Exception e) {
					}
				}
			}
		}
		else if ("back".equals(identifier)) {
			if(level == 1)
			{
				Kuix.getFrameHandler().removeFrame(instance);
				return false;
			}
			else
			{
				level--;
				try {
					CategoryList precate = (CategoryList) preCategory.get(new Integer(level));
					categorylist = mdp.getCategoryList(precate.categoryCode);
					mdp.setCategoryList(categorylist);
					screen.setTitle(precate.categoryName);
					screen.setDataProvider(mdp);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} 
		else if ("arrLeft".equals(identifier)) {
			if (currPage > 1) currPage--;
			try {
				changePageValue(currPage);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if ("arrRight".equals(identifier)) {
			if(isnext)
			{
				currPage++;
				try {
					changePageValue(currPage);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return true;
	}
	public void onRemoved() {
		try {
			currPage =1;
			MainFrame.instance.showScreen();
			screen.cleanUp();
			screen = null;
		} catch (Exception e) {
		}
	}
	public void changePageValue(int page) throws Exception {
		Text textPage = (Text)screen.getWidget("textPage");
		textPage.setText(currPage + "/" + totalpage);
		Picture pictureLeft = (Picture) screen.getWidget("pic-left");
		Picture pictureRight = (Picture) screen.getWidget("pic-right");
		try {
			if (page == 1) {
				pictureLeft.setImage(Image.createImage("/img/left-dis.png"));
			} else {
				pictureLeft.setImage(Image.createImage("/img/left.png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			// get data form 
			categorylist = mdp.getCategoryListWithPage(code,currPage,pageSize);
			
			if(categorylist.length !=0)
			{
				mdp.setCategoryList(categorylist);
			}
			// display
			
			if (page == totalpage) {
				isnext = false;
				pictureRight.setImage(Image.createImage("/img/right-dis.png"));
			} else {
				isnext = true;
				pictureRight.setImage(Image.createImage("/img/right.png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}